package account

import (
    "fmt"

    "iip/account/model"
    dict "iip/dictionary/model"
)

func (s *Account) CacheGetAccountByID(id int64) (*model.Account, error) {
    return s.GetAccountByID(id)
}


func (s *Account) CacheGetAccountRoles(id int64) ([]*model.Role, error) {
    return s.GetAccountRoles(id)
}

func (s *Account) GetAccountByID(id int64) (*model.Account, error) {
    u := model.Account{}
    err := s.gorm().Where("id = ?", id).First(&u).Error
    return &u, err
}

func (s *Account) GetAccountByName(name string) (*model.Account, error) {
    u := model.Account{}
    err := s.gorm().Where("name = ?", name).First(&u).Error
    return &u, err
}

func (s *Account) GetAccountByPhone(phone string) (*model.Account, error) {
    u := model.Account{}
    err := s.gorm().Where("phone = ?", phone).First(&u).Error
    return &u, err
}

func (s *Account) GetAccountByWXOpenID(id string) (*model.Account, error) {
    u := model.Account{}
    err := s.gorm().Where("wx_openid = ?", id).First(&u).Error
    return &u, err
}

func (s *Account) GetAuth(uid int64, auth_type int) (*model.AccountAuth, error) {
    a := model.AccountAuth{}
    err := s.gorm().Where("account_id = ? AND type = ?", uid, auth_type).First(&a).Error
    return &a, err
}

func (s *Account) GetResourceByID(uid int64) (*model.Resource, error) {
    a := model.Resource{}
    err := s.gorm().Where("id = ?", uid).First(&a).Error
    return &a, err
}

func (s *Account) GetResourceByName(name string) (*model.Resource, error) {
    a := model.Resource{}
    err := s.gorm().Where("name = ?", name).First(&a).Error
    return &a, err
}

func (s *Account) GetRoleByID(uid int64) (*model.Role, error) {
    a := model.Role{}
    err := s.gorm().Where("id = ?", uid).First(&a).Error
    return &a, err
}

func (s *Account) GetAllRole() ([]*model.Role, error) {
    lst := []*model.Role{}
    if err := s.gorm().Find(&lst).Error; err != nil {
        return nil, err
    }
    return lst, nil
}

func (s *Account) GetRoleByName(n string) (*model.Role, error) {
    a := model.Role{}
    err := s.gorm().Where("name = ?", n).First(&a).Error
    return &a, err
}


func (s *Account) GetRoleResources(id int64) ([]*model.Resource, error) {







    var role_res_list []*model.RoleResource
    if err := s.gorm().Where(&model.RoleResource{RoleID: id}).Find(&role_res_list).Error; err != nil {
        return nil, err
    }
    var res_id_list []string
    for _, r := range role_res_list {
        res_id_list = append(res_id_list, fmt.Sprint(r.ResourceID))
    }


    var res_list []*model.Resource = []*model.Resource{}
    if err := s.gorm().Model(&model.Resource{}).Where("`id` IN (?)", res_id_list).Find(&res_list).Error; err != nil {
        return nil, err
    }
    return res_list, nil
}

func (s *Account) GetAllResources() ([]*model.Resource, error) {

    var res_list []*model.Resource = []*model.Resource{}
    if err := s.gorm().Find(&res_list).Error; err != nil {
        return nil, err
    }
    return res_list, nil
}


func (s *Account) GetAccountRoles(id int64) ([]*model.Role, error) {

    var acc_role_list []*model.AccountRole
    if err := s.gorm().Where(&model.AccountRole{AccountID: id}).Find(&acc_role_list).Error; err != nil {
        return nil, err
    }
    var role_id_list []string
    for _, r := range acc_role_list {
        role_id_list = append(role_id_list, fmt.Sprint(r.RoleID))
    }


    var role_list []*model.Role = []*model.Role{}
    if err := s.gorm().Model(&model.Role{}).Where("`id` IN (?)", role_id_list).Find(&role_list).Error; err != nil {
        return nil, err
    }
    return role_list, nil
}

func (s *Account) GetAccountRoleByID(uid int64) (*model.Role, error) {
    a := model.Role{}
    err := s.gorm().Where("id = ?", uid).First(&a).Error
    return &a, err
}

func (s *Account) GetRoleResourceByID(uid int64) (*model.RoleResource, error) {
    a := model.RoleResource{}
    err := s.gorm().Where("id = ?", uid).First(&a).Error
    return &a, err
}

func (s *Account) account_detail(acc *model.Account) interface{} {

    com, err := s.dict.GetCompanyByID(acc.CompanyID)
    if err != nil {
        s.logger.Error(err)
        com = nil
    }


    role_list, err := s.GetAccountRoles(acc.ID)
    if err != nil {
        s.logger.Error(err)
        role_list = []*model.Role{}
    }

    return &struct {
        model.Account
        Company  *dict.DictCompany `json:"company"`
        RoleList []*model.Role     `json:"role_list"`
    }{
        Account:  *acc,
        Company:  com,
        RoleList: role_list,
    }
}
